package components{
	
	import flash.events.ContextMenuEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;
	import flash.geom.Point;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;
	
	import model.xpdl2.Activity;
	
	import mx.collections.ArrayList;
	import mx.containers.Panel;
	import mx.controls.Button;
	import mx.core.Application;
	import mx.managers.PopUpManager;
	
	import util.ConfigUtil;
	import util.Constants;
	import util.EventPopupWindow;
	
	/**
	 * @author sunkai
	 * 流程中的节点, 如Service, Script等.
	 * */
	public class ActivityLabel extends Button{
		
		private var config:ConfigUtil
		
		private var isDrive:Boolean=false;
		
		private var isMonitor:Boolean=false;
		
		private var model:Activity;
		
		private var startLocation:Point;
		
		private var inputLines:ArrayList;
		
		private var outputLines:ArrayList;
		
		private var app:Application;
		
		private var drivePanel:Panel;
		
		private var monitorPanel:Panel;
		
		private var driveBtn:MediatorButton;
		
		private var monitorBtn:MediatorButton;
		
		private var setDrive: ContextMenuItem;
		
		private var clearDrive: ContextMenuItem;
		
		private var setMonitor: ContextMenuItem;
		
		private var clearMonitor: ContextMenuItem;
		
		/** private var _x:Number;
		
		private var _y:Number;
		
		private var _width:Number;
		
		private var _height:Number; 
		**/
		
		public function ActivityLabel(){
			
			super();
			
			createChildren();
			this.
			initialize();
			initContextMenu();
			
			this.addEventListener(MouseEvent.MOUSE_DOWN, pickup);
			this.addEventListener(MouseEvent.MOUSE_UP,place);

			inputLines=new ArrayList();
			outputLines=new ArrayList();
			
		}
		
		public function setConfig(v:ConfigUtil){
			this.config=v;
		}
		
		public function initContextMenu(){
		
			this.contextMenu= new ContextMenu();
			this.contextMenu.hideBuiltInItems();
			
			
			setDrive = new ContextMenuItem("设置驱动交互点");
			clearDrive = new ContextMenuItem("清除驱动交互点");
			setMonitor = new ContextMenuItem("设置监听交互点");
			clearMonitor = new ContextMenuItem("清除监听交互点");
			
			setDrive.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, setDriveDispose);
			clearDrive.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, clearDriveDispose);
			setMonitor.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, setMonitorDispose);
			clearMonitor.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, clearMonitorDispose);
			
			this.contextMenu.customItems.push(setDrive);
			//this.contextMenu.customItems.push(clearDrive);
			this.contextMenu.customItems.push(setMonitor);
			//this.contextMenu.customItems.push(clearMonitor);			
		
		}
		
		public function redraw(){
			this.drawDrive(this);
			this.drawMonitor(this);
		}
		
		private function rebuildContextMenu(){
			
			this.contextMenu= new ContextMenu();
			this.contextMenu.hideBuiltInItems();
			
			if(isDrive && isMonitor){
				this.contextMenu.customItems.push(clearDrive);
				this.contextMenu.customItems.push(clearMonitor);
			}else if(!isDrive && isMonitor){
				this.contextMenu.customItems.push(setDrive);
				this.contextMenu.customItems.push(clearMonitor);
			}else if(isDrive && !isMonitor){
				this.contextMenu.customItems.push(clearDrive);
				this.contextMenu.customItems.push(setMonitor);
			}else{
				this.contextMenu.customItems.push(setDrive);
				this.contextMenu.customItems.push(setMonitor);
			}
		}
		
		public function setModel(value:Activity){
			
			this.model=value;
			
		}
		
		public function getModel():Activity{
			
			return this.model;
			
		}
		
		public function setApp(a:Application){
		  		
		  		this.app=a;
		  		
		}
		  
		  public function getApp():Application{
		  
		  		return this.app;
		  
		  }
		
		public function setDriveDispose(event:Event){
			
				isDrive=true;	
				config.setActivityId(this.model.id);
				config.setActivityName(this.model.name);
				var panel:EventPopupWindow = new EventPopupWindow(config);
                panel.title = "选择触发的事件类型";  
                 	               	
                PopUpManager.addPopUp(panel, this.getApp(), true);
				PopUpManager.centerPopUp(panel);
			    PopUpManager.bringToFront(panel);		
				drawDrive(this);				
				rebuildContextMenu();
				
		}
		
		public function clearDriveDispose(event:Event){
			
				drivePanel.graphics.clear();
				isDrive=false;
				rebuildContextMenu();
				
		}
		
		private function drawDrive(value:ActivityLabel){
				
				if(isDrive){
					
					drivePanel.graphics.clear();
					var label:ActivityLabel=value;
					drivePanel.graphics.lineStyle(5, 0x0000FF);
					var glabelPoint:Point=label.localToGlobal(new Point(label.x, label.y));
					var gmediatorPoint:Point=driveBtn.localToGlobal(new Point(driveBtn.x, driveBtn.y));
					drivePanel.graphics.moveTo(label.x+label.width/2, label.y + Constants.TOOBAR_HEIGHT+Constants.PROMPT_HEIGHT+Constants.CANVAS_TITLE_Y );
					drivePanel.graphics.lineTo(label.x+label.width/2, gmediatorPoint.y+ driveBtn.height/2 );
					drivePanel.graphics.lineTo(gmediatorPoint.x-5,gmediatorPoint.y+ driveBtn.height/2);//Constants.TOOBAR_Y+Constants.TOOBAR_HEIGHT+Constants.MEDITOR_TITLE_Y+driveBtn.parent.y+driveBtn.y+driveBtn.height/2 );
				
					drivePanel.graphics.beginFill(0x0000FF);
					drivePanel.graphics.moveTo(gmediatorPoint.x,gmediatorPoint.y+ driveBtn.height/2);
					drivePanel.graphics.lineTo(gmediatorPoint.x-10,gmediatorPoint.y+ driveBtn.height/2-8);
					drivePanel.graphics.lineTo(gmediatorPoint.x-10,gmediatorPoint.y+ driveBtn.height/2+8);
					drivePanel.graphics.lineTo(gmediatorPoint.x,gmediatorPoint.y+ driveBtn.height/2);
					drivePanel.graphics.endFill();
					
				}
		}
		
		public function setMonitorDispose(event:Event){
			
				isMonitor=true;
				drawMonitor(this);
				rebuildContextMenu();
				
		}
		
		private function drawMonitor(value:ActivityLabel){
				
				if(isMonitor){
					
					monitorPanel.graphics.clear();
					var label:ActivityLabel=value;
					monitorPanel.graphics.lineStyle(5, 0xFF0000);
					var glabelPoint:Point=label.localToGlobal(new Point(label.x, label.y));
					var gmediatorPoint:Point=monitorBtn.localToGlobal(new Point(monitorBtn.x, monitorBtn.y));
					monitorPanel.graphics.moveTo(label.x+label.width/2, label.y + label.height + Constants.TOOBAR_HEIGHT+Constants.PROMPT_HEIGHT+Constants.CANVAS_TITLE_Y );
					monitorPanel.graphics.lineTo(label.x+label.width/2, gmediatorPoint.y+ monitorBtn.height/2 );
					monitorPanel.graphics.lineTo(gmediatorPoint.x,gmediatorPoint.y+ monitorBtn.height/2)//Constants.TOOBAR_Y+Constants.TOOBAR_HEIGHT+Constants.MEDITOR_TITLE_Y+driveBtn.parent.y+driveBtn.y+driveBtn.height/2 );
				
					monitorPanel.graphics.beginFill(0xFF0000);
					
					if(label.y + label.height + Constants.TOOBAR_HEIGHT+Constants.PROMPT_HEIGHT+Constants.CANVAS_TITLE_Y > gmediatorPoint.y+ monitorBtn.height/2){
						monitorPanel.graphics.moveTo(label.x+label.width/2, label.y + label.height + Constants.TOOBAR_HEIGHT+Constants.PROMPT_HEIGHT+Constants.CANVAS_TITLE_Y);
						monitorPanel.graphics.lineTo(label.x+label.width/2-8, label.y + label.height + Constants.TOOBAR_HEIGHT+Constants.PROMPT_HEIGHT+Constants.CANVAS_TITLE_Y-10);
						monitorPanel.graphics.lineTo(label.x+label.width/2+8, label.y + label.height + Constants.TOOBAR_HEIGHT+Constants.PROMPT_HEIGHT+Constants.CANVAS_TITLE_Y-10);
						monitorPanel.graphics.lineTo(label.x+label.width/2, label.y + label.height + Constants.TOOBAR_HEIGHT+Constants.PROMPT_HEIGHT+Constants.CANVAS_TITLE_Y);
					}else{
						monitorPanel.graphics.moveTo(label.x+label.width/2, label.y + label.height + Constants.TOOBAR_HEIGHT+Constants.PROMPT_HEIGHT+Constants.CANVAS_TITLE_Y);
						monitorPanel.graphics.lineTo(label.x+label.width/2-8, label.y + label.height + Constants.TOOBAR_HEIGHT+Constants.PROMPT_HEIGHT+Constants.CANVAS_TITLE_Y+10);
						monitorPanel.graphics.lineTo(label.x+label.width/2+8, label.y + label.height + Constants.TOOBAR_HEIGHT+Constants.PROMPT_HEIGHT+Constants.CANVAS_TITLE_Y+10);
						monitorPanel.graphics.lineTo(label.x+label.width/2, label.y + label.height + Constants.TOOBAR_HEIGHT+Constants.PROMPT_HEIGHT+Constants.CANVAS_TITLE_Y);
					}
					monitorPanel.graphics.endFill();
					
				}
				
		}
		
		public function clearMonitorDispose(event:Event){
			
			monitorPanel.graphics.clear();			
			isMonitor=false;
			rebuildContextMenu();

		}
		
		public function pickup(event:Event):void{
			
			startLocation=new Point();
			startLocation.x=event.target.x;
			startLocation.y=event.target.y;
			event.target.startDrag();
			event.target.filters=[new DropShadowFilter()];
			var i:int;
			
			for(i=0;i<this.outputLines.length;i++){
				var line:ActivityLine=ActivityLine(outputLines.getItemAt(i));
				line.startX = this.x + this.width;
				line.startY = this.y + this.height / 2;
				line.draw();
			}
			
			for(i=0;i<this.inputLines.length;i++){
				var line:ActivityLine=ActivityLine(inputLines.getItemAt(i));
				line.endX = this.x;
				line.endY = this.y + this.height / 2;
				line.draw();
			}
			
			this.drawDrive(this);
			this.drawMonitor(this);
			
			//setChildIndex(DisplayObject(event.target),numChildren-1);
			
		}

		public function place(event:Event):void{
			event.target.stopDrag();
			event.target.filters=null;
			
			var i:int;
			
			for(i=0;i<this.outputLines.length;i++){
				var line:ActivityLine=ActivityLine(outputLines.getItemAt(i));
				line.startX = this.x + this.width;
				line.startY = this.y + this.height / 2;
				line.draw();
			}
			
			for(i=0;i<this.inputLines.length;i++){
				var line:ActivityLine=ActivityLine(inputLines.getItemAt(i));
				line.endX = this.x;
				line.endY = this.y + this.height / 2;
				line.draw();
			}
			
			this.drawDrive(this);
			this.drawMonitor(this);
			
		}
			
		
		public function addInputLine(value:ActivityLine){
			this.inputLines.addItem(value);
		}
		
		public function addOutputLine(value:ActivityLine){
			this.outputLines.addItem(value);
		}
		
		public function getInputLines():ArrayList{
			return this.inputLines;
		}
		
		public function getOutputLines():ArrayList{
			return this.outputLines;
		}
		
		public function setDrivePanel(value:Panel){
			this.drivePanel=value;
		}
		
		public function getDrivePanel():Panel{
			return this.drivePanel;
		}
		
		public function setMonitorPanel(value:Panel){
			this.monitorPanel=value;
		}
		
		public function getMonitorPanel():Panel{
			return this.monitorPanel;
		}
		
		public function setDriveBtn(value:MediatorButton){
			this.driveBtn=value;
		}
		
		public function getDriveBtn():MediatorButton{
			return this.driveBtn;
		}
		
		public function setMonitorBtn(value:MediatorButton){
			this.monitorBtn=value;
		}
		
		public function getMonitorBtn():MediatorButton{
			return this.monitorBtn;
		}
		
		/* public function get x():Number{
			return this._x;
		}
		
		public function set x(value:Number):void{
			this._x=value;
		}
		
		public function get y():Number{
			return this._y;
		}
		
		public function set y(value:Number):void{
			this._y=value;
		}
		
		public function get width():Number{
			return this._width;
		}
		
		public function set width(value:Number):void{
			this._width=value;
		}
		
		public function get height():Number{
			return this._height;
		}
		
		public function set height(value:Number):void{
			this._height=value;
		} */
		
	}
}